package org.easy.db.generator;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.easy.db.generator.converter.ColumnTypeConverter;
import org.easy.db.generator.converter.JavaColumnTypeConverter;
import org.easy.db.generator.util.FieldUtil;

import java.util.Map;

/**
 * 字段定义表
 * @Description:
 * @Author: wxl
 * @CreateTime: 2023/1/19 10:27
 */
@Data
public class ColumnDefinition {

    private static ColumnTypeConverter converter = new JavaColumnTypeConverter();

    private String columnName;

    private String dataType;

    private String columnKey;

    private String columnComment;

    private String extra;

    private String javaType;

    private boolean isKey;

    private boolean isHasExtra;

    private String javaColumnName;

    private String javaPackage;

    public ColumnDefinition () {}

    public ColumnDefinition (Map<String,Object> columnMap) {
        this();
        this.columnName = (String) columnMap.get("columnName");
        this.dataType = (String)columnMap.get("dataType");
        this.columnKey = (String)columnMap.get("columnKey");
        this.columnComment = (String)columnMap.get("columnComment");
        this.extra = (String)columnMap.get("extra");
        this.initOtherParam();
    }

    private void initOtherParam() {
        setHasExtra("auto_increment".equals(getExtra()));
        setKey("PRI".equals(getColumnKey()));
        String javaType = converter.convertTypeBox(getDataType());
        setJavaType(javaType);
        String javaColumn = FieldUtil.underlineFilter(getColumnName());
        setJavaColumnName(javaColumn);
        String javaPackage = ColumnType.getPkg(getDataType());
        if (StringUtils.isNotEmpty(javaPackage)) {
            setJavaPackage(javaPackage);
        } else {
            setJavaPackage("Nothing");
        }
    }

    public boolean isNotNothing () {
        return !"Nothing".equals(this.javaPackage);
    }

}
